package com.qianclass.talklive.app.view;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianclass.talklive.R;

import java.sql.Date;
import java.text.SimpleDateFormat;

/**
 *计时器控件
 */

public class TimerView extends FrameLayout
{

    private TextView mTvQuit;
    private LongTextView roomtitle;
    private ImageView mIvIndicator;
    private ImageView mIvSettings;
    private ImageView mIvStartTime;
    private TextView mTvTime;
    private ImageView mIvNotice;
    private TextView mTvDate;
    private TextView mTvCountTime;

    public TimerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 加载布局
        /*平板-手机*/
        int resLayout = isTabDevice() ? R.layout.popwindow_pad : R.layout.popwindow;
        LayoutInflater.from(context).inflate(resLayout, this);
        initView();
    }

    private boolean isTabDevice() {
        return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    private void initView() {
        if (isTabDevice()) {
            //平板设备
            mIvStartTime = (ImageView) findViewById(R.id.iv_start_time);
            mTvTime = (TextView) findViewById(R.id.tv_time);
            mIvNotice = (ImageView) findViewById(R.id.iv_notice);
            mTvDate = (TextView) findViewById(R.id.tv_date);

        }

        mIvIndicator = (ImageView) findViewById(R.id.iv_indicator);
        mTvQuit = (TextView) findViewById(R.id.tv_quit);
        mIvSettings = (ImageView) findViewById(R.id.iv_settings);
        roomtitle = (LongTextView) findViewById(R.id.roomname);
        mTvCountTime = (TextView) findViewById(R.id.tv_count_time);
    }

    // 添加退出点击事件
    public void setQuitListener(OnClickListener listener) {
        mTvQuit.setOnClickListener(listener);
    }

    //点击settings按钮的监听
    public void setSettingsListener(OnClickListener listener) {
        mIvSettings.setOnClickListener(listener);
    }

    // 设置标题的方法
    public void setTitleText(String title) {
        roomtitle.setText(title);
    }

    public void setImage(int resId) {
        mIvIndicator.setImageResource(resId);
    }

    public void setTime(String hour, String minute, String second) {
        mTvCountTime.setText(hour + ":" + minute + ":" + second);
        mTvCountTime.setTextColor(getResources().getColor(R.color.timer_red));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    /**
     * 是否开启计时
     * @param toggle
     */
    public void setToggleTimeCount(boolean toggle){
        mIvStartTime.setImageResource(toggle ? R.drawable.stop_time : R.drawable.start_time);
    }

    public void setTimerListener(OnClickListener listener) {
        mIvStartTime.setOnClickListener(listener);
    }

    /**
     * 设置时间
     * @param time
     */
    public void setDate(String time){
        mDataTime = Long.parseLong(time) * 1000;

        formatDate();

        postDelayed(mTask, 1000);
    }

    private void formatDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date(mDataTime));
        String[] strs = format.split("-| ");
        mTvDate.setText(strs[1] + getResources().getString(R.string.course_tool_label_month) +
                                strs[2] + getResources().getString(R.string.course_tool_label_day));
        mTvTime.setText(strs[3]);
    }

    private long mDataTime;
    public Runnable mTask = new Runnable() {
        @Override
        public void run() {
            mDataTime += 1000;
            formatDate();
            //延时循环任务
            postDelayed(mTask, 1000);
        }
    };

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        removeCallbacks(mTask);
    }
}
